﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections;
using System.Linq;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace Galaktika.BI.Silverlight.Runtime.Data.Olap
{
    public class FlatDescription : INotifyPropertyChanged
    {
        public FlatDescription()
        {
        }

        internal FlatDescription(object argument)
        {
            this.ArgumentSegments.Add(argument);
        }

        /// <summary>
        /// Contains x value display.
        /// </summary>
        public readonly List<object> ArgumentSegments = new List<object>();

        /// <summary>
        /// Containes x value unique name.
        /// </summary>
        public readonly ICollection<string> UniqueNameSegments = new List<string>();

        //private object m_Argument;
        public object Argument
        {
            get
            {
                List<string> buff = new List<string>(this.ArgumentSegments.Count);
                this.ArgumentSegments.ForEach(o => buff.Add(o.ToString()));
                return string.Join(".", buff.ToArray());
            }
        }

        private object displayValue;
        public object DisplayValue
        {
            get 
            { 
                return displayValue;
            }
            set
            {
                displayValue = value;
                OnPropertyChanged("DisplayValue");
            }
        }

        private double m_ZValue;
        public double ZValue
        {
            get { return m_ZValue; }
            set { m_ZValue = value; }
        }


        //private object unigueName;
        public object UniqueName
        {
            get 
            {
                return string.Join(",", this.UniqueNameSegments.ToArray());
            }
            //set
            //{
            //    unigueName = value;
            //    OnPropertyChanged("UniqueName");
            //}
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Fires the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">Property that changed.</param>
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    public class FlatDescriptionCollection : IEnumerable<FlatDescription>
    {
        public const string ArgumentPath = "Argument";
        public const string DisplayValuePath = "DisplayValue";
        public const string UniqueNamePath = "UniqueNamePath";


        internal List<FlatDescription> itemSource;

        public FlatDescriptionCollection()
        {
            this.itemSource = new List<FlatDescription>();
        }

        public FlatDescription[] ToValueArray()
        {
            FlatDescription[] valueArray = new FlatDescription[itemSource.Count];
            int i = 0;
            foreach (var item in itemSource)
            {
                double arg;
                valueArray[i] = new FlatDescription();
                valueArray[i].DisplayValue = item.DisplayValue;
                object argSeg = null;
                if (Double.TryParse(item.Argument.ToString(), out arg))
                    argSeg = arg; //valueArray[i].Argument
                //valueArray[i].Argument = i*10;
                argSeg = i * 10;
                valueArray[i].ArgumentSegments.Add(argSeg);
                i++;
            }
            return valueArray;
        }
        //public string SeriesName { get; set; }
       
        public FlatDescription this[int index]
        {
            get
            {
                if (itemSource.Count > index)
                    return itemSource[index];
                return null;
            }
        }

        public FlatDescription this[object argument]
        {
            get
            {
                foreach (var description in itemSource)
                {
                    if (description.Argument.Equals(argument))
                        return description;
                }
                return null;
            }
        }

        public void Add(FlatDescription value)
        {
            if (value != null)
                this.itemSource.Add(value);
        }

        public static IEnumerable<FlatDescription> TestData
        {
            get
            {
                var coll = new List<FlatDescription>();
                //coll.Add(new FlatDescription() { Argument = "Brest", DisplayValue = 430000 });
                //coll.Add(new FlatDescription() { Argument = "Vitebsk", DisplayValue = 220000 });
                //coll.Add(new FlatDescription() { Argument = "Grodno", DisplayValue = 470000 });
                //coll.Add(new FlatDescription() { Argument = "Gomel", DisplayValue = 580000 });
                //coll.Add(new FlatDescription() { Argument = "Minsk", DisplayValue = 1600000 });
                //coll.Add(new FlatDescription() { Argument = "Mogilev", DisplayValue = 380000 });
                coll.Add(new FlatDescription("Brest") { DisplayValue = 430000 });
                coll.Add(new FlatDescription("Vitebsk") { DisplayValue = 220000 });
                coll.Add(new FlatDescription("Grodno") { DisplayValue = 470000 });
                coll.Add(new FlatDescription("Gomel") { DisplayValue = 580000 });
                coll.Add(new FlatDescription("Minsk") { DisplayValue = 1600000 });
                coll.Add(new FlatDescription("Mogilev") { DisplayValue = 380000 });
                return coll;
            }
        }        
    
        #region IEnumerable<FlatDescription> Members

        IEnumerator<FlatDescription>  IEnumerable<FlatDescription>.GetEnumerator()
        {
            return this.itemSource.GetEnumerator();
        }

        #endregion
    
        #region IEnumerable Members

        IEnumerator  IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)this.itemSource).GetEnumerator();
        }

        #endregion
}
}
